//
//  MainViewController.m
//  CocoaSMS4iPhone
//
//  Created by Sooyong Wang on 09. 05. 15.
//  Copyright __MyCompanyName__ 2009. All rights reserved.
//

#import "MainViewController.h"
#import "MainView.h"

@implementation MainViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
        // Custom initialization
			xpeed = [[Xpeed alloc] init];
    }
    return self;
}


// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
	[sendButtton setEnabled:NO];
}


/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning]; // Releases the view if it doesn't have a superview
    // Release anything that's not essential, such as cached data
}


- (void)dealloc {
    [super dealloc];
}

- (IBAction) refresh:(id)sender
{
	[xpeed login];
	remainingSMSCountUILabel.text = [NSString stringWithFormat:@"%@", [xpeed valueForKey:@"remainingSMSCount"]];
	if ( [[xpeed valueForKey:@"remainingSMSCount"] intValue] > 0 ) {
		[sendButtton setEnabled:YES];
	}
}
- (IBAction) send:(id)sender
{
	[xpeed setValue:senderPhoneUITF.text forKey:@"senderPhone"];
	[xpeed setValue:receiverPhoneUITF.text forKey:@"receiverPhone"];
	[xpeed setValue:messageUITF.text forKey:@"message"];
	
	[xpeed sendSMS];
	
	messageUITF.text = @"";
	
	[self refresh:self];
}
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
	[xpeed setValue:messageUITF.text forKey:@"message"];
	int bytes = [xpeed messageBytes];
	if ( bytes >= 80 ) {
		return NO;
	}
	dataBytes.text = [NSString stringWithFormat:@"%d",bytes];
	return YES;
}
@end
